//
//  WCUnsavedFilesWindowController.m
//  WabbitCode
//
//  Created by William Towe on 2/8/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "WCUnsavedFilesWindowController.h"
#import "WCMacros.h"
#import "WCProject.h"
#import "WCAppController.h"
#import "WCFile.h"
#import "NSArray+WCExtensions.h"


@interface WCUnsavedFilesWindowController (Private)
- (void)_performSetup;
@end

@implementation WCUnsavedFilesWindowController
- (id)init {
	return [self initWithWindowNibName:@"WCUnsavedFilesWindow"];
}

- (void)dealloc {
	[_windowTitle release];
	[_headerTitle release];
	[_headerSubTitle release];
	[_headerImage release];
	[_unsavedFiles release];
	[super dealloc];
}

- (void)windowDidLoad {
	[super windowDidLoad];
	
	[_tableView selectAll:nil];
}

@synthesize windowTitle=_windowTitle,headerTitle=_headerTitle,headerSubTitle=_headerSubTitle,headerImage=_headerImage,unsavedFiles=_unsavedFiles, type=_type;

+ (NSInteger)runModalForUnsavedFiles:(NSArray *)unsavedFiles type:(WCUnsavedFilesType)type; {
	if (![unsavedFiles count])
		return NSOKButton;
	
	WCUnsavedFilesWindowController *controller = [[[[self class] alloc] init] autorelease];
	
	[controller setType:type];
	[controller setUnsavedFiles:unsavedFiles];
	
	[controller _performSetup];
	
	return [NSApp runModalForWindow:[controller window]];
}

- (IBAction)dontSaveAction:(id)sender; {
	[[self window] close];
	[NSApp stopModalWithCode:NSOKButton];
}
- (IBAction)cancelAction:(id)sender; {
	[[self window] close];
	[NSApp stopModalWithCode:NSCancelButton];
}
- (IBAction)saveSelectedAction:(id)sender; {
	[[[_tableView dataSource] selectedObjects] makeObjectsPerformSelector:@selector(saveTextFile)];
	[[self window] close];
	[NSApp stopModalWithCode:NSOKButton];
}

- (void)_performSetup; {
	[self setWindowTitle:([[self unsavedFiles] count] == 1)?NSLocalizedString(@"Unsaved File",@"Unsaved File"):NSLocalizedString(@"Unsaved Files",@"Unsaved Files")];
	switch ([self type]) {
		case WCUnsavedFilesTypeBeforeBuilding:
			[self setHeaderTitle:NSLocalizedString(@"Save Before Building?",@"Save Before Building?")];
			[self setHeaderImage:[NSImage imageNamed:@"Build48x48"]];
			break;
		case WCUnsavedFilesTypeBeforeQuitting:
			[self setHeaderTitle:NSLocalizedString(@"Save Before Quitting?",@"Save Before Quitting?")];
			[self setHeaderImage:[NSImage imageNamed:@"NSApplicationIcon"]];
			break;
		case WCUnsavedFilesTypeBeforeClosingProject:
			[self setHeaderTitle:NSLocalizedString(@"Save Before Closing Project?",@"Save Before Closing Project?")];
			[self setHeaderImage:[[NSWorkspace sharedWorkspace] iconForFileType:kWCProjectFileExtension]];
			break;
		case WCUnsavedFilesTypeBeforeClosingFile:
			[self setHeaderTitle:NSLocalizedString(@"Save Before Closing?",@"Save Before Closing?")];
			[self setHeaderImage:[[_unsavedFiles firstObject] fileIcon]];
			break;
		default:
			break;
			
	}
	[self setHeaderSubTitle:([[self unsavedFiles] count] == 1)?NSLocalizedString(@"There is 1 file with unsaved changes",@"There is 1 file with unsaved changes"):[NSString stringWithFormat:NSLocalizedString(@"There are %u files with unsaved changes",@"There are %u files with unsaved changes"), [[self unsavedFiles] count]]];
}
@end
